home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
- *
- * WMemory
- *
- *************************************************************************/
-
- #ifndef _WMEMORY_HPP_INCLUDED
- #define _WMEMORY_HPP_INCLUDED
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma pack(push,8);
- #pragma enum int;
- #endif
-
- #ifndef _WDEF_HPP_INCLUDED
- # include "wdef.hpp"
- #endif
- #ifndef _WLLIST_HPP_INCLUDED
- # include "wllist.hpp"
- #endif
-
- #include <new.h>
-
- //
- // Memory tracking flags
- //
-
- #define WMEMORY_CHECKFOROVERWRITE 0x00000001L
- #define WMEMORY_TRACKMEMORY 0x00000002L
- #define WMEMORY_TRAPNOMEMORY 0x00000004L
- #define WMEMORY_WALKHEAPS 0x00000008L
- #define WMEMORY_USE_HEAPALLOC 0x00000010L
- #define WMEMORY_PRINT_ALL 0x80000000L
- #define WMEMORY_ZERO_MEMORY 0x40000000L
-
- #define WMEMORY_ALLCHECKS (WMEMORY_CHECKFOROVERWRITE|\
- WMEMORY_TRACKMEMORY|\
- WMEMORY_WALKHEAPS)
-
- #define WMEMORY_NOCHECKS 0x00000000L
-
- //
- // WMemoryAllocator -- Use this structure to define the low-level
- // C-type functions used to allocate memory.
- // You must declare the structure statically.
- //
-
- typedef void * WHeapHandle;
-
- typedef void * __stdcall (*WHeapAllocator)( WHeapHandle heap, WULong memSize );
- typedef void * __stdcall (*WHeapReallocator)( WHeapHandle heap, void *mem,
- WULong newSize );
- typedef WBool __stdcall (*WHeapDeallocator)( WHeapHandle heap, void *mem );
- typedef WBool __stdcall (*WHeapValidator)( WHeapHandle heap, void *mem );
-
- typedef WHeapHandle __stdcall (*WHeapCreator)( WULong initialSize,
- WULong maximumSize );
- typedef WBool __stdcall (*WHeapDestructor)( WHeapHandle heap );
-
- struct WCMCLASS WMemoryHeap {
- wllist_link link;
- const WChar *heapName;
- WHeapHandle heap;
- WULong initialSize;
- WULong maximumSize;
-
- WHeapAllocator allocate;
- WHeapDeallocator deallocate;
- WHeapReallocator reallocate;
- WHeapValidator validate;
- WHeapCreator create;
- WHeapDestructor destruct;
- };
-
- //
- // Memory dumping -- for debugging purposes.
- //
-
- typedef WBool WCMDEF (*WMemoryDumper)( void *memory,
- const WChar **memoryType,
- WChar *buffer, WULong len );
-
- #ifdef _DEBUG
- #define WAttachDumper(mem,dump) WMemory::AttachDumpRoutine((void*)mem,dump)
- #else
- #define WAttachDumper(mem,dump)
- #endif
-
- //
- // WMemory
- //
-
- class WCMCLASS WMemory {
-
- public:
-
- /**********************************************************
- * Constructors and Destructors
- *********************************************************/
-
- WMemory();
-
- virtual ~WMemory();
-
- /**********************************************************
- * Static properties
- *********************************************************/
-
- // ArrayHeap
-
- static WMemoryHeap *GetArrayHeap();
- static WBool SetArrayHeap( WMemoryHeap *heap );
-
- // CurrentMemoryUsage
-
- static WULong GetCurrentMemoryUsage();
-
- // GlobalNewHandler
- //
- // Sets a global new_handler value for the library.
-
- static PFV SetGlobalNewHandler( PFV new_handler );
-
- // Heap
-
- static WMemoryHeap *GetHeap();
- static WBool SetHeap( WMemoryHeap *heap );
-
- // NewHandler
- //
- // Sets the C++ new_handler value for the library.
- // Returns the old new_handler. You should call this
- // if you call set_new_handler in your application
- // to make sure that the library is using the same
- // new_handler.
-
- static PFV SetNewHandler( PFV new_handler );
-
- // PeakMemoryUsage
-
- static WULong GetPeakMemoryUsage();
-
- /**********************************************************
- * Static methods
- *********************************************************/
-
- // Allocate
- //
- // Allocate memory from a given heap. If no heap
- // specified, use the thread default.
-
- static void *Allocate( WULong size, WBool array,
- WMemoryHeap *allocator=NULL );
- static void *Allocate( WULong size, WBool array,
- const WChar *functionName,
- const WChar *fileName, WULong line,
- WMemoryHeap *allocator=NULL );
-
- // AttachDumpRoutine
- //
- // After a piece of memory has been allocated, call
- // this routine to attach a specific dump routine to it.
-
- static WBool AttachDumpRoutine( void *memory,
- WMemoryDumper dumpRoutine );
-
- // Deallocate
- //
- // Deallocate a specific piece of memory.
-
- static WBool Deallocate( void *memory, WBool array );
- static WBool Deallocate( void *memory, WBool array,
- const WChar *functionName,
- const WChar *fileName, WULong line );
-
- // Reallocate
- //
- // Reallocate a specific piece of memory.
-
- static void *Reallocate( void *memory, WULong newSize,
- WBool array );
- static void *Reallocate( void *memory, WULong newSize,
- WBool array,
- const WChar *functionName,
- const WChar *fileName, WULong line );
-
- // Validate
- //
- // Check to see if a piece of memory is valid. If NULL
- // is passed, checks whole heap. Only implemented in
- // the debugging DLL.
-
- static WBool Validate( void *memory, WMemoryHeap *allocator );
-
- /**********************************************************
- * Internal
- *********************************************************/
-
- // LogNextDelete
- //
- // Set the location that the next delete will come from.
- // Used in debugging mode with memory tracking.
-
- static WBool LogNextDelete( void *ptr, const WChar *functionName,
- const WChar *fileName, WULong line );
-
- // FreeDeleteLog
- //
- // Free the delete log for a thread. Call occasionally.
-
- static WBool FreeDeleteLog();
-
- /**********************************************************
- * Data members
- *********************************************************/
-
- public:
- };
-
- //
- // Use these macros to redefine new & delete
- //
-
- #ifndef _NO_REDEFINE_NEW
-
- #define WREDEFINENEW \
- void *operator new( size_t size ) \
- { return WMemory::Allocate( size, FALSE ); } \
- void *operator new( size_t size, const WChar *funcName, \
- const WChar *fileName, WULong line ) \
- { return WMemory::Allocate( size, FALSE, funcName, \
- fileName, line ); } \
- void *operator new( size_t, void *p ) { return p; } \
- void *operator new []( size_t size ) \
- { return WMemory::Allocate( size, TRUE ); } \
- void *operator new []( size_t size, const WChar *funcName,\
- const WChar *fileName, WULong line ) \
- { return WMemory::Allocate( size, TRUE, funcName,\
- fileName, line ); } \
- void *operator new []( size_t, void *p ) { return p; }
-
- #define WREDEFINEDELETE \
- void operator delete( void *p ) { WMemory::Deallocate( p, FALSE ); } \
- void operator delete []( void *p ) { WMemory::Deallocate( p, TRUE ); }
-
- #else
-
- #define WREDEFINENEW
- #define WREDEFINEDELETE
-
- #endif
-
- #define W_REDEFINENEW WREDEFINENEW
- #define W_REDEFINEDELETE WREDEFINEDELETE
-
- #ifndef _WNO_PRAGMA_PUSH
- #pragma enum pop;
- #pragma pack(pop);
- #endif
-
- #endif // _WMEMORY_HPP_INCLUDED
-
-